<?php
class UsersController extends AppController {
	var $name = "Users";
	var $helpers = array('Html','Form');

	function beforeFilter() {
		$this->__validateLoginStatus();
	}
	
	function home() {
		
	}
	
	function register() {
		if (!empty($this->params['form'])) {
			if ($this->User->save($this->params['form'])) {
				$this->flash('Your registration information was accepted.','/users/register');
			} 
			else {
				$this->flash('There was a problem with your registration','/users/register');
			}
		}
	}
	function login() {
		if(!empty($this->params['form'])) {	
//		if(!empty($this->data)) {
			$user = $this->User->validateLogin($this->params['form']);
            if(!empty($user)) {
            	$this->set('title_for_layout','Welcome to Synergy Forex Pty Ltd - Backoffice Application - Login Successful');
            	$this->Session->write('name', $user["name"]);
            	$this->Session->setFlash('You\'ve successfully logged in.');
                $this->redirect('home');
                exit();
            }
            else {
                $this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
                exit();
            }
        }
  	}
	function logout() {
		$this->Session->destroy('user');
        $this->Session->setFlash('You\'ve successfully logged out.');
        $this->redirect('login'); 
	}

	function __validateLoginStatus() {
		 if($this->action != 'login' && $this->action != 'logout' && $this->action != 'register') {
            if($this->Session->check('User') == false) {
                //$this->redirect('login');
                $this->Session->setFlash('The URL you\'ve followed requires you login.');
            }
        } 
	}

}
?>
